package ciyuan.Item.SkillBook;

import ciyuan.Entity.EntityRegisterManager;
import ciyuan.Entity.ExplosionTrap.ExplosionTrapEntity;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class ExplosionTrap extends SkillBookType {

    public ExplosionTrap() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 20 * 1000);

        BlockPos blockPos = SkillUtils.getPlayerSee(playerEntity.level, playerEntity, RayTraceContext.FluidMode.SOURCE_ONLY, 7).getBlockPos();
        ExplosionTrapEntity entity = new ExplosionTrapEntity(EntityRegisterManager.explosiontrap.get(), playerEntity.level);
        entity.moveTo(blockPos.getX(), blockPos.getY() + 2, blockPos.getZ());
        SkillUtils.addEntity(entity, playerEntity.level);

        BlockPos blockPos1 = getPlayerSee1(playerEntity.level,playerEntity, RayTraceContext.FluidMode.SOURCE_ONLY,7).getBlockPos();
        ExplosionTrapEntity entity1 = new ExplosionTrapEntity(EntityRegisterManager.explosiontrap.get(),playerEntity.level);
        entity1.moveTo(blockPos1.getX(),blockPos1.getY()+2,blockPos1.getZ());
        SkillUtils.addEntity(entity1,playerEntity.level);

        BlockPos blockPos2 = getPlayerSee2(playerEntity.level,playerEntity, RayTraceContext.FluidMode.SOURCE_ONLY,7).getBlockPos();
        ExplosionTrapEntity entity2 = new ExplosionTrapEntity(EntityRegisterManager.explosiontrap.get(),playerEntity.level);
        entity2.moveTo(blockPos2.getX(),blockPos2.getY()+2,blockPos2.getZ());
        SkillUtils.addEntity(entity2,playerEntity.level);

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    public static BlockRayTraceResult getPlayerSee1(World world, PlayerEntity playerEntity, RayTraceContext.FluidMode p_219968_2_, int distance) {
        float f = playerEntity.xRot;
        float f1 = playerEntity.yRot-50;
        Vector3d vector3d = playerEntity.getEyePosition(1.0F);
        float f2 = MathHelper.cos(-f1 * ((float) Math.PI / 180F) - (float) Math.PI);
        float f3 = MathHelper.sin(-f1 * ((float) Math.PI / 180F) - (float) Math.PI);
        float f4 = -MathHelper.cos(-f * ((float) Math.PI / 180F));
        float f5 = MathHelper.sin(-f * ((float) Math.PI / 180F));
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        Vector3d vector3d1 = vector3d.add((double) f6 * (double) distance, (double) f5 * (double) distance, (double) f7 * (double) distance);
        return world.clip(new RayTraceContext(vector3d, vector3d1, RayTraceContext.BlockMode.OUTLINE, p_219968_2_, playerEntity));
    }

    public static BlockRayTraceResult getPlayerSee2(World world, PlayerEntity playerEntity, RayTraceContext.FluidMode p_219968_2_, int distance) {
        float f = playerEntity.xRot;
        float f1 = playerEntity.yRot+50;
        Vector3d vector3d = playerEntity.getEyePosition(1.0F);
        float f2 = MathHelper.cos(-f1 * ((float) Math.PI / 180F) - (float) Math.PI);
        float f3 = MathHelper.sin(-f1 * ((float) Math.PI / 180F) - (float) Math.PI);
        float f4 = -MathHelper.cos(-f * ((float) Math.PI / 180F));
        float f5 = MathHelper.sin(-f * ((float) Math.PI / 180F));
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        Vector3d vector3d1 = vector3d.add((double) f6 * (double) distance, (double) f5 * (double) distance, (double) f7 * (double) distance);
        return world.clip(new RayTraceContext(vector3d, vector3d1, RayTraceContext.BlockMode.OUTLINE, p_219968_2_, playerEntity));
    }

    @Override
    public String getSkillCastName() {
        return "ExplosionTrap";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.20");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.20");
    }
}
